<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" label-width="100px" :model="queryParams" :inline="true">
            <el-form-item label="承租人名称" prop="lesseeName">
              <el-input v-model="queryParams.lesseeName" placeholder="请输入承租人名称" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="车&nbsp;&nbsp;牌&nbsp;&nbsp;号" prop="licensePlateNumber">
              <el-input v-model="queryParams.licensePlateNumber" placeholder="请输入车牌号" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="车辆品牌" prop="vehicleBrand">
              <el-input v-model="queryParams.vehicleBrand" placeholder="请输入车辆品牌" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <div style="text-align: right">
          <el-button type="warning" icon="Download" @click="handleExport">导出</el-button>
          <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()">删除</el-button>
        </div>
      </template>

      <el-table v-loading="loading" :data="carhistoryRentalList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="主键标识" align="center" prop="id" v-if="false" />
        <el-table-column label="汽车基础信息标识" align="center" prop="automobileBasicInfoId" v-if="false" />
        <el-table-column label="汽车租赁人标识" align="center" prop="carRentalInfoId" v-if="false" />
        <el-table-column label="承租人名称" align="center" prop="lesseeName" />
        <el-table-column label="开始时间" align="center" prop="startTime" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="结束时间" align="center" prop="endTime" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="退租时间" align="center" prop="rentOutTime" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.rentOutTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="折合日租金" align="center" prop="equivalentDailyRent" />
        <el-table-column label="车&nbsp;&nbsp;牌&nbsp;&nbsp;号" align="center" prop="licensePlateNumber" />
        <el-table-column label="车辆品牌" align="center" prop="vehicleBrand" />
        <el-table-column label="车辆类型" align="center" prop="vehicleType">
          <template #default="scope">
            <dict-tag :options="car_vehicle_type" :value="scope.row.vehicleType" />
          </template>
        </el-table-column>
        <el-table-column label="所&nbsp;&nbsp;有&nbsp;&nbsp;人" align="center" prop="possessor" />
        <el-table-column label="型&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;号" align="center" prop="modeNumber" />
        <el-table-column label="动力类型" width="120" align="center" prop="dynamicType">
          <template #default="scope">
            <dict-tag :options="car_dynamic_type" :value="scope.row.dynamicType" />
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-button link type="primary" @click="handelOpen('history', scope.row.carRentalInfoId, scope.row.automobileBasicInfoId)">
              详情
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
    </el-card>
    <formDialog ref="formRef" />
  </div>
</template>

<script setup name="CarhistoryRental" lang="ts">
import {
  listCarhistoryRental,
  getCarhistoryRental,
  delCarhistoryRental,
  addCarhistoryRental,
  updateCarhistoryRental
} from '@/api/zichanguanli/carhistoryRental';
import { CarhistoryRentalVO, CarhistoryRentalQuery, CarhistoryRentalForm } from '@/api/zichanguanli/carhistoryRental/types';
import formDialog from '../carbasicInformation/form';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { car_vehicle_type, car_dynamic_type } = toRefs<any>(proxy?.useDict('car_vehicle_type', 'car_dynamic_type'));

const carhistoryRentalList = ref<CarhistoryRentalVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const carhistoryRentalFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const initFormData: CarhistoryRentalForm = {};
const data = reactive<PageData<CarhistoryRentalForm, CarhistoryRentalQuery>>({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    lesseeName: undefined,
    licensePlateNumber: undefined,
    vehicleBrand: undefined,
    params: {}
  },
  rules: {}
});

const { queryParams, form, rules } = toRefs(data);
const formRef = ref();
const handelOpen = (type: string, carRentalInfoId?: number, automobileBasicInfoId?: number) => {
  formRef.value.open(type, carRentalInfoId, automobileBasicInfoId);
};

/** 查询汽车租赁历史列表 */
const getList = async () => {
  loading.value = true;
  const res = await listCarhistoryRental(queryParams.value);
  carhistoryRentalList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
};

/** 表单重置 */
const reset = () => {
  form.value = { ...initFormData };
  carhistoryRentalFormRef.value?.resetFields();
};

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};

/** 多选框选中数据 */
const handleSelectionChange = (selection: CarhistoryRentalVO[]) => {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
};

/** 新增按钮操作 */
const handleAdd = () => {
  reset();
  dialog.visible = true;
  dialog.title = '添加汽车租赁历史';
};

/** 修改按钮操作 */
const handleUpdate = async (row?: CarhistoryRentalVO) => {
  reset();
  const _id = row?.id || ids.value[0];
  const res = await getCarhistoryRental(_id);
  Object.assign(form.value, res.data);
  dialog.visible = true;
  dialog.title = '修改汽车租赁历史';
};

/** 提交按钮 */
const submitForm = () => {
  carhistoryRentalFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.id) {
        await updateCarhistoryRental(form.value).finally(() => (buttonLoading.value = false));
      } else {
        await addCarhistoryRental(form.value).finally(() => (buttonLoading.value = false));
      }
      proxy?.$modal.msgSuccess('操作成功');
      dialog.visible = false;
      await getList();
    }
  });
};

/** 删除按钮操作 */
const handleDelete = async (row?: CarhistoryRentalVO) => {
  const _ids = row?.id || ids.value;
  await proxy?.$modal.confirm('确定删除所选的信息吗？').finally(() => (loading.value = false));
  await delCarhistoryRental(_ids);
  proxy?.$modal.msgSuccess('删除成功');
  await getList();
};

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download(
    'zichanguanli/carhistoryRental/export',
    {
      ...queryParams.value
    },
    `carhistoryRental_${new Date().getTime()}.xlsx`
  );
};

onMounted(() => {
  getList();
});
</script>
